package system

import (
	"github.com/beego/beego/v2/client/orm"
	"ltbz/cultural_tourism_api/utils"
	"time"
)

// 角色表
type SysRole struct {
	RoleId     int64     `orm:"column(role_id);pk"`
	RoleName   string    `description:"角色名称"`
	CreateTime time.Time `description:"创建时间"`
	ModifyTime time.Time `description:"修改时间"`
}

type SysRoleAddReq struct {
	RoleName string `description:"角色名称"`
}

// 新增
func (r *SysRole) Add() (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(r)
	return
}

//// 修改
//func (r *SysRole) Update(updateCols []string) (err error) {
//	err = global.DEFAULT_MYSQL.Model(r).Select(updateCols).Updates(r).Error
//	return
//}

type SysRoleEditReq struct {
	RoleId   int64  `description:"角色id"`
	RoleName string `description:"角色名称"`
}

type SysRoleDelReq struct {
	RoleId int64 `description:"角色id"`
}

type SysRoleMenuEditReq struct {
	RoleId     int64   `description:"角色id"`
	MenuIdList []int64 `description:"绑定的菜单ID"`
}

func (r *SysRole) GetRoleByRoleId(roleId int64) (item *SysRole, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_role WHERE role_id=? `
	err = o.Raw(sql, roleId).QueryRow(&item)
	return
}

func (r *SysRole) GetRoleByRoleName(roleName string) (item *SysRole, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_role WHERE role_name=? `
	err = o.Raw(sql, roleName).QueryRow(&item)
	return
}

type SysRoleListItem struct {
	RoleId     int64  `description:"角色id"`
	RoleName   string `description:"角色名称"`
	CreateTime string `description:"创建时间"`
	ModifyTime string `description:"最后更新时间"`
}

func (r *SysRole) GetSysRolePageCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM sys_role WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (r *SysRole) GetSysRolePage(condition string, pars []interface{}, startSize, pageSize int) (items []*SysRoleListItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_role WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY create_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type SysRolePageResp struct {
	List   []*SysRoleListItem
	Paging *utils.PagingResp
}

//func (r *SysRole) GetRoleNameList() (list []*SysRole, err error) {
//	err = global.DEFAULT_MYSQL.Model(r).Find(&list).Error
//	return
//}

type SysRoleListReq struct {
	RoleName string `description:"角色名称"`
	utils.PagingReq
}

type SysRoleMenuListReq struct {
	RoleId int64 `description:"角色id"`
}

type SysRoleMenuResp struct {
	List       []*MenuListItemResp
	ChoiceList []int64
}

// 删除
func (r *SysRole) Delete() (err error) {
	o := orm.NewOrm()
	sql := ` DELETE FROM sys_role WHERE role_id=? `
	_, err = o.Raw(sql, r.RoleId).Exec()
	return
}

type SysRoleUserListReq struct {
	RoleName string `description:"角色名称"`
}

func (r *SysRole) SysRoleUserListPage(condition string, pars []interface{}) (items []*SysUserListItem, err error) {
	o := orm.NewOrm()
	sql := ` SELECT a.*,b.role_name FROM sys_user AS a
			INNER JOIN sys_role AS b ON a.role_id=b.role_id
			WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY a.create_time DESC `
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

type SysRoleAllReq struct {
	RoleName string `description:"角色名称"`
}

func (r *SysRole) GetSysRoleAll(condition string, pars []interface{}) (items []*SysRoleListItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_role WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY create_time DESC `
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
